import http from '../utils/http';

const BASE_URL = 'http://localhost:8082'; // 后端API地址

/**
 * 获取所有会议室列表
 * @returns {Promise<AjaxResult>} 返回会议室列表数据
 * @typedef {Object} AjaxResult
 * @property {number} code - 状态码 200成功，500失败
 * @property {string} msg - 提示信息
 * @property {Array<Room>} data - 会议室数据
 */
export const getAllRooms = async () => {
  try {
    const response = await http.get('/rooms');
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 分页获取会议室列表
 * @param {number} pageNum - 当前页码
 * @param {number} pageSize - 每页数量
 * @returns {Promise<AjaxResult>} 返回分页会议室数据
 */
export const getRoomsByPage = async (pageNum = 1, pageSize = 10) => {
  try {
    const response = await http.get(`/rooms/page?pageNum=${pageNum}&pageSize=${pageSize}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 条件分页查询会议室
 * @param {Object} params - 查询参数
 * @param {number} params.pageNum - 当前页码
 * @param {number} params.pageSize - 每页数量
 * @param {Object} params.condition - 查询条件
 * @param {string} [params.condition.roomname] - 会议室名称
 * @param {number} [params.condition.roomType] - 会议室类型
 * @param {number} [params.condition.capacity] - 容纳人数
 * @param {number} [params.condition.projector] - 投影仪状态
 * @param {number} [params.condition.sound] - 音响状态
 * @param {number} [params.condition.internet] - 网络状态
 * @param {number} [params.condition.status] - 会议室状态
 * @returns {Promise<AjaxResult>} 返回分页会议室数据
 */
export const getRoomsByCondition = async (params) => {
  const { pageNum = 1, pageSize = 10, condition = {} } = params;
  try {
    const response = await http.post(`/rooms/page/condition?pageNum=${pageNum}&pageSize=${pageSize}`, condition);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 创建新会议室
 * @param {Object} roomData - 会议室数据
 * @param {string} roomData.roomname - 会议室名称
 * @param {number} roomData.roomType - 会议室类型 (1:教室, 2:圆桌, 3:董事会, 4:多媒体)
 * @param {number} roomData.capacity - 容纳人数
 * @param {number} roomData.projector - 投影仪状态 (0:异常, 1:正常)
 * @param {number} roomData.sound - 音响状态 (0:异常, 1:正常)
 * @param {number} roomData.internet - 网络状态 (0:异常, 1:正常)
 * @param {number} roomData.price - 价格/小时
 * @param {number} roomData.status - 状态 (0:未使用, 1:使用中)
 * @returns {Promise<AjaxResult>} 返回创建结果
 */
export const createRoom = async (roomData) => {
  try {
    const response = await http.post('/rooms', roomData);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 更新会议室信息
 * @param {number} roomId - 会议室ID
 * @param {Object} roomData - 会议室更新数据
 * @returns {Promise<AjaxResult>} 返回更新结果
 */
export const updateRoom = async (roomId, roomData) => {
  try {
    const response = await http.put(`/rooms/${roomId}`, roomData);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 删除会议室
 * @param {number} roomId - 会议室ID
 * @returns {Promise<AjaxResult>} 返回删除结果
 */
export const deleteRoom = async (roomId) => {
  try {
    const response = await http.delete(`/rooms/${roomId}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 获取单个会议室详情
 * @param {number} roomId - 会议室ID
 * @returns {Promise<AjaxResult>} 返回会议室详情
 */
export const getRoomById = async (roomId) => {
  try {
    const response = await http.get(`/rooms/${roomId}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 获取单个会议室指定日期的排班详情
 * @param {number} roomId - 会议室ID
 * @param {string} date - 查询日期 (YYYY-MM-DD 格式)
 * @returns {Promise<AjaxResult>} 返回会议室排班详情
 */
export const getRoomScheduleByDate = async (roomId, date) => {
  try {
    const response = await http.get(`/rooms/${roomId}/schedule?date=${date}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 根据条件筛选会议室
 * @param {Object} searchCriteria - 筛选条件
 * @param {number} [searchCriteria.capacity] - 容纳人数
 * @param {number} [searchCriteria.projector] - 投影仪状态 (0:异常, 1:正常)
 * @param {number} [searchCriteria.sound] - 音响状态 (0:异常, 1:正常)
 * @param {number} [searchCriteria.internet] - 网络状态 (0:异常, 1:正常)
 * @param {string} [searchCriteria.startTime] - 开始时间 (ISO 8601 格式)
 * @param {string} [searchCriteria.endTime] - 结束时间 (ISO 8601 格式)
 * @returns {Promise<AjaxResult>} 返回符合条件的会议室列表数据
 */
export const searchRooms = async (searchCriteria) => {
  try {
    const response = await http.post('/rooms/search', searchCriteria);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 获取当前时间所有会议室的信息列表（包含当前时间的状态）
 * @returns {Promise<AjaxResult>} 返回会议室列表数据
 */
export const getRoomsNowStatus = async () => {
  try {
    const response = await http.get('/rooms/now');
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 获取该房间当日的所有时间段的状态
 * @param {number} roomId - 房间ID
 * @returns {Promise<AjaxResult>} 返回房间当日所有时间段的状态数据
 */
export const getRoomHourlyStatus = async (roomId) => {
  try {
    const response = await http.get(`/rooms/hourly/status/${roomId}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 根据会议室名称查找会议室ID
 * @param {string} roomname - 会议室名称
 * @returns {Promise<AjaxResult<List<Map<String, Object>>>>} 返回匹配的会议室ID列表
 */
export const searchRoomIdsByRoomName = async (roomname) => {
  try {
    console.log('Searching room IDs for roomname:', roomname);
    const response = await http.get(`${BASE_URL}/rooms/search/roomid?roomname=${encodeURIComponent(roomname)}`);
    console.log('Room ID search response:', response.data);
    return response.data;
  } catch (error) {
    console.error('搜索会议室ID出错:', error);
    throw error;
  }
};
